import Rx from "rxjs"

//模拟缓存数据
let subject1 = new Rx.BehaviorSubject("").map(value => { console.log(value); return { num4: value }; })
subject1.next(1);
let subject2 = new Rx.BehaviorSubject("").map(value => { console.log(value); return { num5: value }; })
subject2.next(3);

let observable1 = buildObservable("No.1", 1000).map(value => { console.log(value); return { num1: value }; })
let observable2 = buildObservable("No.2", 2000).map(value => { console.log(value); return { num2: value }; })
let observable3 = buildObservable("No.3", 3000).map(value => { console.log(value); return { num3: value }; })

//模拟cometd更新数据
setTimeout(() => {
    subject1.next(2);
}, 3500);

function buildObservable(name, timeout) {
    return Rx.Observable.create(observer => {
        setTimeout(() => {
            observer.next("name is: " + name);
        }, timeout)
    });
}


let mySource = Rx.Observable.merge(subject1, subject2)
    .scan((acc, value) => {
        return Object.assign({}, acc, value);
    }, {})
    .filter(value => {
        return /* value.num1 != undefined && value.num2 != undefined && value.num3 != undefined && */ value.num4 != undefined && value.num5 != undefined
    });

//模拟等待所有数据都回来后渲染表格
let subscribtion;
new Promise((resolve, reject) => {
    subscribtion = mySource.subscribe(value => {
        resolve(value);
        subscribtion.unsubscribe();
    })
})
    .then((x) => {
        console.log("promise:" + JSON.stringify(x));
    });
